<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>流程管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body class="childrenBody">

<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索开始-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <!-- 表单开始 -->
                <form class="layui-form" id="searchForm" method="post">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">流程名称:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="deploymentName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn search_btn">查询</a>
                            <button type="reset" class="layui-btn layui-btn-warm">清空</button>
                        </div>
                    </div>
                </form>
                <!-- 表单结束 -->
            </div>
        </fieldset>
        <!--搜索结束-->
    </div>
</div>

<div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
    <form class="layui-form" method="post" id="frm">
        <div class="layui-form-item">
            <label class="layui-form-label">流程名称</label>
            <div class="layui-input-block">
                <input type="text" name="deploymentName" lay-verify="required" autocomplete="off"
                       placeholder="请输入流程名称" id="deploymentName" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">流程图文件</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-normal" id="uploadProcess">请选择流程图文件</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <a class="layui-btn" href="javascript:void(0)" lay-submit="" id="addWorkFlow"
               lay-filter="addWorkFlow">提交</a>
            <button type="reset" class="layui-btn layui-btn-warm">重置</button>
        </div>
    </form>
</div>

<div id="viewProcessImage" style="display: none;padding: 0.3125rem">
    <img alt="流程图" id="processImage"/>
</div>

<table id="deploymentList" lay-filter="deploymentList"></table>
<table id="processDefinitionList" lay-filter="processDefinitionList"></table>

<!--表头工具条-->
<script type="text/html" id="deploymentTableToolBar">
    <a class="layui-btn layui-btn layui-btn" lay-event="add">添加</a>
    <a class="layui-btn layui-btn layui-btn-danger" lay-event="batchDel">批量删除</a>
</script>

<!--表内操作-->
<script type="text/html" id="deploymentListBar">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="viewProcessImage">查看流程图</a>
</script>

<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>
<script>
    var tableDeploymentIns;
    var tableprocessDefinitionIns;

    layui.use(['form', 'layer', 'table', 'laydate', 'laytpl', 'upload'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;
        var upload = layui.upload;
        // 第一个表
        //流程部署列表
        tableDeploymentIns = table.render({
            elem: '#deploymentList',
            url: api + 'workFlow/loadAllDeployment',
            cellMinWidth: true,
            toolbar: '#deploymentTableToolBar',
            page: true,
            height: "full-350",
            limits: [5, 10, 15, 20],
            limit: 5,
            id: "deploymentListTable",
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'id', title: '部署ID', minWidth: 100, align: "center"},
                {field: 'name', title: '部署名称', minWidth: 100, align: "center"},
                {field: 'deploymentTime', title: '部署时间', minWidth: 100, align: "center"},
                {title: '操作', minWidth: 175, templet: '#deploymentListBar', fixed: "right", align: "center"}
            ]]
        });

        // 第二个表
        //流程定义列表
        tableprocessDefinitionIns = table.render({
            elem: '#processDefinitionList',
            url: api + 'workFlow/loadAllProcessDefinition',
            cellMinWidth: true,
            page: true,
            height: "full-380",
            limits: [5, 10, 15, 20],
            limit: 5,
            id: "processDefinitionListTable",
            cols: [[
                {field: 'id', title: '流程定义ID', minWidth: 100, align: "center"},
                {field: 'name', title: '流程定义名称', minWidth: 100, align: "center"},
                {field: 'key', title: '流程定义KEY', minWidth: 100, align: "center"},
                {field: 'version', title: '流程定义版本', minWidth: 100, align: "center"},
                {field: 'deploymentId', title: '部署ID', minWidth: 100, align: "center"},
                {field: 'resourceName', title: '资源文件名[bpmn]', minWidth: 100, align: "center"},
                {field: 'diagramResourceName', title: '资源文件名[png]', minWidth: 100, align: "center"}
            ]]
        });

        //查询
        $(".search_btn").on("click", function () {
            var params = $("#searchForm").serialize();
            table.reload('deploymentListTable', {
                url: api + 'workFlow/loadAllDeployment?' + params
            });
            table.reload('processDefinitionListTable', {
                url: api + 'workFlow/loadAllProcessDefinition?' + params
            });
        });

        //监听头工具栏事件
        table.on('toolbar(deploymentList)', function (obj) {
            switch (obj.event) {
                case 'add':
                    toAddWorkFlow();
                    break;
                case 'batchDel':
                    batchDelete();
                    break;
            }
            ;
        });

        var index;

        //打开 部署流程的弹出层
        function toAddWorkFlow() {
            index = layui.layer.open({
                title: "添加流程",
                type: 1,
                area: ["600px", "300px"],
                content: $("#addOrUpdateDiv"),
                success: function (layer, index) {
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回流程列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            });

            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(index);
            })
        }

        //选完文件后不自动上传
        upload.render({
            elem: '#uploadProcess'
            , url: api + 'workFlow/addWorkFlow'
            , auto: false
            , accept: 'file'//选择上传文件
            , acceptMime: 'application/zip'//打开文件选择框默认显示压缩文件
            , exts: 'zip'//校验类型
            , field: 'mf'
            , data: {
                deploymentName: function () {
                    return $('#deploymentName').val();
                }
            }
            , bindAction: '#addWorkFlow'
            , done: function (data) {
                layer.msg(data.msg);
                layui.layer.close(index);
                tableDeploymentIns.reload();
                tableprocessDefinitionIns.reload();
            }
            , error: function () {
                layer.msg('部署失败');
                layui.layer.close(index);
            }
        });

        //列表操作
        table.on('tool(deploymentList)', function (obj) {
            var layEvent = obj.event,
                data = obj.data;
            if (layEvent === 'del') { //删除
                layer.confirm('确定删【' + data.name + '】流程吗？', {icon: 3, title: '提示信息'}, function (index) {
                    $.post(api + "workFlow/deleteWorkFlow", {
                        deploymentId: data.id  //将需要删除的id作为参数传入
                    }, function (data) {
                        layer.msg(data.msg);
                        //刷新table
                        tableDeploymentIns.reload();
                        tableprocessDefinitionIns.reload();
                        //关闭提示框
                        layer.close(index);
                    })
                });
            } else if (layEvent === "viewProcessImage") {
                //传入流程对象
                viewProcessImage(data);
            }
        });

        //批量删除
        function batchDelete() {
            //得到当前表格里面的checkbox的选中对象集合
            var checkStatus = table.checkStatus('deploymentListTable'),//选中状态
                data = checkStatus.data;//选中的对象集
            var ids = "a=1";
            if (data.length > 0) {
                for (var i in data) {
                    ids += "&ids=" + data[i].id;
                }
                layer.confirm('确定删除选中的流程？', {icon: 3, title: '提示信息'}, function (index) {
                        $.post(api + "workFlow/batchDeleteWorkFlow?" + ids, function (data) {
                            layer.msg(data.msg);
                            //刷新table
                            tableDeploymentIns.reload();
                            tableprocessDefinitionIns.reload();
                            //关闭提示框
                            layer.close(index);
                        })
                })
            } else {
                layer.msg("请选择需要删除的流程");
            }
        }

        //查看流程图
        function viewProcessImage(data) {
            var index = layui.layer.open({
                title: "查看[" + data.name + "]流程图",
                type: 1,
                area: ["500px", "600px"],
                content: $("#viewProcessImage"),
                success: function (layero, index) {
                    $("#processImage").attr('src', api + 'workFlow/viewProcessImage?deploymentId=' + data.id);

                    setTimeout(function () {
                        layui.layer.tips('点击此处返回流程列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            });
        }
    });
</script>
</body>
</html>
